package com.usian.article.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.fegin.BehaviorFegin;
import com.usian.article.fegin.UserFeign;
import com.usian.article.mapper.ApArticleConfigMapper;
import com.usian.article.mapper.ApArticleContentMapper;
import com.usian.article.mapper.ApCollectionMapper;
import com.usian.article.mapper.AuthorMapper;
import com.usian.article.service.ArticleInfoService;
import com.usian.model.article.*;
import com.usian.model.behavior.dtos.FindlikeDto;
import com.usian.model.behavior.dtos.FindunlikeDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApLikesBehavior;
import com.usian.model.behavior.pojos.ApUnlikesBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.dtos.FindUaFDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserFollow;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ArticleInfoServiceImpl implements ArticleInfoService {
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    /**
     * 加载文章详情
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        Map<String,Object> resultMap=new HashMap<>();
        //参数校验
        if (dto == null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.NULL_CANSHU);
        }
        //2.查询文章的配置
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));
        if(apArticleConfig == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //3.查询文章的内容
        if(!apArticleConfig.getIsDelete()&& !apArticleConfig.getIsDown()){
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));
            resultMap.put("content",apArticleContent);
        }
        resultMap.put("config",apArticleConfig);
        //封装结果
        ResponseResult result = ResponseResult.okResult(resultMap);
        return result;
    }

    @Autowired
    private UserFeign userFeign;
    @Autowired
    BehaviorFegin behaviorFegin;
    @Autowired
    ApCollectionMapper apCollectionMapper;
    @Autowired
    private AuthorMapper authorMapper;
    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        //1.检查参数
        if(dto == null || dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询行为实体
        ApUser user = AppThreadLocalUtils.getUser();

        ApBehaviorEntry apBehaviorEntry = behaviorFegin.findByUserIdOrEquipmentId(user.getId());
        if(apBehaviorEntry == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        boolean isUnlike=false,isLike = false,isCollection = false,isFollow = false;

        //3.查询不喜欢行为
        FindunlikeDto findunlikeDto = new FindunlikeDto();
        findunlikeDto.setArticleId(dto.getArticleId());
        findunlikeDto.setEntryId(apBehaviorEntry.getId());
        ApUnlikesBehavior apUnlikesBehavior = behaviorFegin.findUnLikeByArticleIdAndEntryId(findunlikeDto);
        if(apUnlikesBehavior != null && apUnlikesBehavior.getType() == ApUnlikesBehavior.Type.UNLIKE.getCode()){
            isUnlike = true;
        }

        //4.查询点赞行为
        FindlikeDto findlikeDto = new FindlikeDto();
        findlikeDto.setArticleId(dto.getArticleId());
        findlikeDto.setEntryId(apBehaviorEntry.getId());
        findlikeDto.setType(ApLikesBehavior.Type.ARTICLE.getCode());
        ApLikesBehavior apLikesBehavior = behaviorFegin.findLikeByArticleIdAndEntryId(findlikeDto);
        if(apLikesBehavior != null && apLikesBehavior.getOperation() == ApLikesBehavior.Operation.LIKE.getCode()){
            isLike = true;
        }

        //5.查询收藏行为
        ApCollection apCollection = apCollectionMapper.selectOne(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getEntryId, apBehaviorEntry.getId())
                .eq(ApCollection::getArticleId, dto.getArticleId()).eq(ApCollection::getType, ApCollection.Type.ARTICLE.getCode()));
        if(apCollection != null){
            isCollection = true;
        }

        //6.查询是否关注
        ApAuthor apAuthor = authorMapper.selectById(dto.getAuthorId());
        FindUaFDto findUaFDto = new FindUaFDto();
        findUaFDto.setFollowId(apAuthor.getUserId());
        findUaFDto.setUserId(user.getId());
        if(apAuthor != null){
            ApUserFollow apUserFollow = userFeign.findByUserIdAndFollowId(findUaFDto);
            if(apUserFollow != null){
                isFollow = true;
            }
        }


        //7.结果返回  {"isfollow":true,"islike":true,"isunlike":false,"iscollection":true}
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("isfollow",isFollow);
        resultMap.put("islike",isLike);
        resultMap.put("isunlike",isUnlike);
        resultMap.put("iscollection",isCollection);
        return ResponseResult.okResult(resultMap);
    }
}
